# coding: utf-8


class HashTable:
    def __init__(self, size):
        self.size = size
        self.slot = [None] * size
        self.data = [None] * size

    def put(self, key, val):
        hash_pos = self.hash_function(key)

        if self.slot[hash_pos] is None:
            self.slot[hash_pos] = key
            self.data[hash_pos] = val
        elif self.slot[hash_pos] == key:
            self.data[hash_pos] = val      # replace
        else:
            next_pos = self.rehash(hash_pos)
            while self.slot[next_pos] is not None and self.slot[next_pos] != key:
                next_pos = self.rehash(next_pos)

            if self.slot[next_pos] is None:
                self.slot[next_pos] = key
                self.data[next_pos] = val
            else:
                self.data[next_pos] = val

    def hash_function(self, key):
        return key % self.size

    def rehash(self, old_pos):
        return (old_pos + 1) % self.size

    def get(self, key):
        start_pos = self.hash_function(key)

        data = None
        found = False
        stop = False
        position = start_pos

        while self.slot[position] is not None and not found and not stop:
            if self.slot[position] == key:
                found = True
                data = self.data[position]
            else:
                position = self.rehash(position)
                if position == start_pos:
                    stop = True
        return data

    def __getitem__(self, item):
        return self.get(item)

    def __setitem__(self, key, value):
        self.put(key, value)

    def delete(self, key):
        pass

    def len(self):
        pass


H = HashTable(11)
H[54] = "cat"
H[26] = "dog"
H[93] = "lion"
H[17] = "tiger"
H[77] = "bird"
H[31] = "cow"
H[44] = "goat"
H[55] = "pig"
H[20] = "chicken"
print(H.slot)
print(H.data)
